/* ========================= */
/* 		ajaxed				 */
var HooAjax = new Class ({
	options: {
	    method: 'post',
	    link: 'ignore',
	    evalScripts: true,
	    data: {'ajax': true}, //should be present (fix for nginx)
	    onSuccess: function (r1, r2, r3, r4) {
	    	if ($defined(this.options.update)) { //HTML response
	    		this.options.postSuccess(); //r3 - html, r4 - js
	    	} else { //JSON response
	    		this.options.postSuccess(r1); //r1 - json, r2 - text
	    	}
	    },
	    postSuccess: function () {},
	    beforeRequest: function() {return true;}
	},
	
	initialize: function(element, options) {
		this.element = element;
		//this.setOptions(this.options, options); //hmm.. it eats "on" (f.e: onSuccess) on merge
		if ($defined(options)) this.options = $merge(this.options, options);
		this.element.addEvents({
			click: this.onClick.bindWithEvent(this)
		});		
	},
	
	onClick: function () {
		if (this.options.beforeRequest(this)) {
			if (!$defined(this.options.url)) this.options.url = this.element.href;
			if ($defined(this.options.update)) { //HTML request
				new Request.HTML(this.options).send();
			} else { //JSON request
				new Request.JSON(this.options).send();
			}
		}
		this.element.blur();
		return false;
	}
})

Element.implement({
    HooAjax: function(options){
        return new HooAjax(this, options);
    }
});

HooAjax.implement(new Events);
HooAjax.implement(new Options);

//example: <a href="/query/whoisonline" rel="ajax" options="{postSuccess: function() {alert('ok');}}">ajax button</a>
// quick init: $$('a[rel=ajax]').each(function(el) {el.HooAjax(JSON.decode(el.getProperty('options')));});